package shop.sales;

import java.io.File;
import java.util.*;

import shop.category.Category;
import shop.seller.Seller;
import date.Date;


//This class set sales randomly.


public class Sales {

	private static final String PATH_OF_SELLER = "../my-db/src/main/resources/seller";
	private static final String PATH_OF_CATEGORY = "../my-db/src/main/resources/products";
	private static final int NUMBER_OF_DAY = 30; // number of days
	private static final int NUMBER_OF_SOLD_PRODUCTS = 4; // number of sold product during one day

	public Sales() {}
	
	public Map<String, List<Map<String, String>>> getMapOfSales() {

		Map<String, List<Map<String, String>>> map = new HashMap<String, List<Map<String, String>>>();
		for (int i = 0; i < NUMBER_OF_DAY; i++)
			map.put(getDate(), getListOfCategory());

		return map;
	}

	private String getDate() {
		Date date = new Date();
		String month = Integer.toString(new Random().nextInt(12));
		String day = Integer.toString(new Random().nextInt(31));

		while (!date.verifyMonth(month) && !date.verifyDay(day)) {
			month = Integer.toString(new Random().nextInt(12));
			day = Integer.toString(new Random().nextInt(31));
		}

		return "2013/" + month + "/" + day;
	}

	public List<Map<String, String>> getListOfCategory() {
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		for (int j = 0; j < NUMBER_OF_SOLD_PRODUCTS; j++) {
			list.add(selectSeller());
			list.add(selectProduct());
		}
		return list;
	}

	private Map<String, String> selectSeller() {
		Seller seller = new Seller(new File(PATH_OF_SELLER));
		Map<Integer, Map<String, String>> mapOfSeller = seller.getMapOfSeller();

		return mapOfSeller.get(getRandom(mapOfSeller.size()));
	}

	private Map<String, String> selectProduct() {
		Category product = new Category(new File(PATH_OF_CATEGORY));
		Map<String, Map<Integer, Map<String, String>>> mapOfProduct = product
				.getMapOfCategory();

		String selectedFile = getFileName();
		int sizeOfMap = mapOfProduct.get(selectedFile).size();

		return mapOfProduct.get(selectedFile).get(getRandom(sizeOfMap));
	}

	private String getFileName() {
		String[] listOfFile = new File(PATH_OF_CATEGORY).list();
		return listOfFile[getRandom(listOfFile.length)];
	}

	private int getRandom(int i) {
		return new Random().nextInt(i);
	}
}
